package com.demo.webmagic.pageProcessor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpHost;

import com.demo.common.constant.ConstantDtoFlight;
import com.demo.common.model.Flightinfo;
import com.demo.common.model.Flightno;
import com.demo.common.model.Flighttrends;
import com.demo.common.model.Proxyip;
import com.demo.common.model._MappingKit;
import com.demo.webmagic.dto.DtoFlightInfo;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class PageProcessorVeryZhunFlighInfo implements PageProcessor {

	private static final Log log = Log.getLog(PageProcessorVeryZhunFlighInfo.class);
	
	private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000).setCycleRetryTimes(3).setCharset("gb2312");
	
	private DtoFlightInfo dtoFlightInfo = new DtoFlightInfo();
	
	public PageProcessorVeryZhunFlighInfo() {
	}
	
	public PageProcessorVeryZhunFlighInfo(Proxyip proxyip) {
		/*List<String[]> httpProxyList = new ArrayList<String[]>();
		if(proxyIpList!=null && proxyIpList.size() > 0) {
			for (Proxyip proxyip : proxyIpList) {
				String[] httpProxy = new String[2];
				httpProxy[0] = proxyip.getIpStr();
				httpProxy[1] = proxyip.getPortStr();
				httpProxyList.add(httpProxy);
			}
		}*/
		HttpHost httpProxy = new HttpHost(proxyip.getIpStr(), Integer.parseInt(proxyip.getPortStr()));
		site.setHttpProxy(httpProxy);
		//site.setHttpProxyPool(httpProxyList);
	}
	public PageProcessorVeryZhunFlighInfo(List<Proxyip> proxyIpList) {
		List<String[]> httpProxyList = new ArrayList<String[]>();
		if(proxyIpList!=null && proxyIpList.size() > 0) {
			for (Proxyip proxyip : proxyIpList) {
				String[] httpProxy = new String[2];
				httpProxy[0] = proxyip.getIpStr();
				httpProxy[1] = proxyip.getPortStr();
				httpProxyList.add(httpProxy);
			}
			site.setHttpProxyPool(httpProxyList);
		}
		//HttpHost httpProxy = new HttpHost("120.52.73.9", 80);
		//site.setHttpProxy(httpProxy);
	}
	
	@Override
	public Site getSite() {
		return site;
	}

	@Override
	public void process(Page page) {
		try {
			//page.putField("jhqf", page.getHtml().xpath("//div[@class='numdap']/div[@class='numtime']/p/text()"));
			String flightStatus = page.getHtml().xpath("//span[@class='red']/text()").toString();
			if(StrKit.isBlank(flightStatus) || flightStatus.trim().equals("null")) {
				dtoFlightInfo.setCode(ConstantDtoFlight.code_fno_invalid);
				dtoFlightInfo.setMessage(ConstantDtoFlight.message_fno_invalid);
				String url = page.getUrl().toString();
				dtoFlightInfo.setFno(url.substring(url.indexOf("=")+1, url.length()));
			} else {
				List<String> fromCityList = page.getHtml().xpath("//div[@class='numdap']/ul/li[@class='num_here']/text()").all();//起飞地点
				List<String> qftimeList = page.getHtml().xpath("//div[@class='numdap']/div[@class='numtime']/p/text()").all();//计划起飞时间、预计起飞时间、实际起飞时间
				List<String> fromWeatherList = page.getHtml().xpath("//div[@class='numdap']/ul/li/div[@class='wea']/table/tbody/tr/td/a/img/@alt").all();//起飞天气
				List<String> fromTemperature = page.getHtml().xpath("//div[@class='numdap']/ul/li/div[@class='wea']/table/tbody/tr/td[@class='f30']/text()").all();//起飞机场温度
				List<String> fromNjdList = page.getHtml().xpath("//div[@class='numdap']/ul/li[@class='njd']/text()").all();//起飞机场能见度
				List<String> fromStateList =  page.getHtml().xpath("//div[@class='numdap']/ul/li[@class='state']/text()").all();//飞机状态详细内容
				
				String fno = page.getHtml().xpath("//div[@class='numinfo']/ul/li[@class='num_here']/text()").toString();//航班号
				List<String> flightStatusList = page.getHtml().xpath("//span[@class='red']/text()").all();
				String company = page.getHtml().xpath("//td[@class='airname']/a/text()").toString();
				String flightType = page.getHtml().xpath("//td[@class='airname' AND @style='padding-left:5px;']/a/text()").toString();
				List<String> zdlList = page.getHtml().xpath("//td[@class='airname' AND @style='padding-left:5px;']/text()").all();
				List<String> planestateList = page.getHtml().xpath("//p[@class='planestate']/text()").all();
				List<String> flightLocationList = page.getHtml().xpath("//p[@class='planestate']/strong/text()").all();//飞机估计位置
				
				List<String> toCityList = page.getHtml().xpath("//div[@class='numarr']/ul/li[@class='num_here']/text()").all();//起飞地点
				List<String> ddtimeList = page.getHtml().xpath("//div[@class='numarr']/div[@class='numtime']/p/text()").all();//计划起飞时间、预计起飞时间、实际起飞时间
				List<String> toWeatherList = page.getHtml().xpath("//div[@class='numarr']/ul/li/div[@class='wea']/table/tbody/tr/td/a/img/@alt").all();//起飞天气
				List<String> toTemperature = page.getHtml().xpath("//div[@class='numarr']/ul/li/div[@class='wea']/table/tbody/tr/td[@class='f30']/text()").all();//起飞机场温度
				List<String> toNjdList = page.getHtml().xpath("//div[@class='numarr']/ul/li[@class='njd']/text()").all();//起飞机场能见度
				List<String> toState =  page.getHtml().xpath("//div[@class='numarr']/ul/li[@class='state']/text()").all();//起飞机场能见度//起飞机场流量
				
				Flightinfo flightInfo = new Flightinfo();
				flightInfo.setFno(fno);
				flightInfo.setCompany(company);
				flightInfo.setFlightType(flightType);
				flightInfo.setUpdateTime(new Date());
				
				List<Flighttrends> trendsList = new ArrayList<Flighttrends>();
				
				if(StrKit.isBlank(fno) || fno.trim().equals("null"))
					throw new RuntimeException("未获取航班信息");
				//System.out.println("使用代理:" + site.getHttpProxy().getHostName() + ":" + site.getHttpProxy().getPort());
				System.out.println("航班号：" + fno);
				System.out.println("所属航空公司：" + company);
				System.out.println("机型:" + flightType);
				System.out.println("**************");
				for (int i = 0; i < fromCityList.size(); i++) {
					
					Flighttrends flightTrends = new Flighttrends();
					
					try {
						flightTrends.setFromCity(fromCityList.get(i));//起飞城市
					} catch (Exception e) {
						
					}
					try {
						String qftime = qftimeList.get(i * 3);
						flightTrends.setJhqfTime(qftime.substring(qftime.indexOf("：") + 1, qftime.length()));//计划起飞时间
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						String yjqftime = qftimeList.get(i * 3 + 1);
						flightTrends.setYjqfTime(yjqftime.substring(yjqftime.indexOf("：") + 1, yjqftime.length()));//预计起飞时间
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						String sjqftime = qftimeList.get(i * 3 + 2);
						flightTrends.setSjqfTime(sjqftime.substring(sjqftime.indexOf("：") + 1, sjqftime.length()));//实际起飞时间
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						flightTrends.setFromWeather(fromWeatherList.get(i));//起飞机场天气
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						flightTrends.setFromWeatherId(0);//这里需要写一个天气转换为标志ID的方法
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						flightTrends.setFromTemperature(fromTemperature.get(i));//起飞机场温度
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						flightTrends.setFromNjd(fromNjdList.get(i));//起飞机场能见度
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						flightTrends.setFromStatus(fromStateList.get(i));//起飞机场状态
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						flightTrends.setFlightStatus(flightStatusList.get(i));//飞机状态
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						flightTrends.setFlightStatusDetail(planestateList.get(i));//飞机状态详情
					} catch (Exception e) {
						// TODO: handle exception
					}
					if(flightLocationList != null && flightLocationList.size() > i) {
						flightTrends.setFlightLocation(flightLocationList.get(i));//飞机大概位置
					}
					
					try {
						flightTrends.setToCity(toCityList.get(i));//到达城市
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						String ddtime = ddtimeList.get(i * 3);
						flightTrends.setJhddTime(ddtime.substring(ddtime.indexOf("：") + 1, ddtime.length()));//计划到达时间
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						String yjddtime = ddtimeList.get(i * 3 + 1);
						flightTrends.setYjddTime(yjddtime.substring(yjddtime.indexOf("：") + 1, yjddtime.length()));//预计到达时间
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						String sjddtime = ddtimeList.get(i * 3 + 2);
						flightTrends.setSjddTime(sjddtime.substring(sjddtime.indexOf("：") + 1, sjddtime.length()));//实际到达时间
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						flightTrends.setToWeather(toWeatherList.get(i));//到达机场天气
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						flightTrends.setToWeatherId(0);//这里需要实现一个天气转换为天气ID的方法
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						flightTrends.setToTemperature(toTemperature.get(i));//到达机场温度
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						flightTrends.setToNjd(toNjdList.get(i));//到达机场能见度
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						flightTrends.setToStatus(toState.get(i));//到达机场状态
					} catch (Exception e) {
						// TODO: handle exception
					}
					try {
						String zdl = zdlList.get(i);
						flightTrends.setZdl(zdl.substring(zdl.lastIndexOf("：") + 1, zdl.length()));
					} catch (Exception e) {}
					trendsList.add(flightTrends);
					/*System.out.println("出发城市:" + fromCityList.get(i));
					System.out.println("计划起飞时间:" + qftimeList.get(i*3));
					System.out.println("预计起飞时间:" + qftimeList.get(i*3+1));
					System.out.println("实际起飞时间:" + qftimeList.get(i*3+2));
					System.out.println("起飞天气：" + fromWeatherList.get(i));
					System.out.println("起飞机场温度：" + fromTemperature.get(i));
					System.out.println("起飞机场能见度：" + fromNjdList.get(i));
					System.out.println("起飞机场状态：" + fromStateList.get(i));
					System.out.println();
					System.out.println("飞机状态：" + flightStatusList.get(i));
					System.out.println("飞机状态详情：" + planestateList.get(i));
					if(flightLocationList != null && flightLocationList.size() > i) {
						System.out.println("飞机大概位置:" + flightLocationList.get(i));
					}
					System.out.println();
					System.out.println("到达城市:" + toCityList.get(i));
					System.out.println("计划到达时间:" + ddtimeList.get(i*3));
					System.out.println("预计到达时间:" + ddtimeList.get(i*3+1));
					System.out.println("实际到达时间:" + ddtimeList.get(i*3+2));
					System.out.println("到达天气：" + toWeatherList.get(i));
					System.out.println("到达机场温度：" + toTemperature.get(i));
					System.out.println("到达机场能见度：" + toNjdList.get(i));
					System.out.println("到达机场状态：" + toState.get(i));
					System.out.println("====================");*/
				}
				dtoFlightInfo.setCode(ConstantDtoFlight.code_success);
				dtoFlightInfo.setFlightInfo(flightInfo);
				dtoFlightInfo.setTrendsList(trendsList);
			}
			page.putField("result", dtoFlightInfo);
		} catch(Exception e) {
			log.error(e.getMessage());
			log.error(page.getUrl().toString());
			dtoFlightInfo.setCode(ConstantDtoFlight.code_fail);
			page.putField("result", dtoFlightInfo);
		}
	}
	
	public DtoFlightInfo getDtoFlightInfo() {
		return dtoFlightInfo;
	}

	public void setDtoFlightInfo(DtoFlightInfo dtoFlightInfo) {
		this.dtoFlightInfo = dtoFlightInfo;
	}

	public static void main(String[] args) {
		PropKit.use("a_little_config.txt");
		DruidPlugin dp = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		_MappingKit.mapping(arp);
		
		dp.start();
		arp.start();
		
		List<Flightno> flightList = Flightno.dao.find("SELECT * FROM flightno limit 300");
		
		String[] urlStrs = new String[flightList.size()];
		
		/*for (int i = 0; i < flightList.size(); i++) {
			urlStrs[i] = "http://www.veryzhun.com/searchnum.asp?flightnum=" + flightList.get(i).getFno();
		}
		
		//查找代理
		List<Proxyip> proxyIpList = Proxyip.dao.find("SELECT * FROM proxyip GROUP BY ipStr ORDER BY valiDate DESC limit 30");
		
		int num = urlStrs.length / proxyIpList.size();

		if(num > 0) {
			for (int i = 0; i < proxyIpList.size(); i++) {
				Proxyip proxyip = proxyIpList.get(i);
				String[] _urlStrs;
				if(i != proxyIpList.size() - 1) {
					_urlStrs = new String[num];
				} else {
					_urlStrs = new String[urlStrs.length - num*(proxyIpList.size()-1)];
				}
				for (int j = 0; j < _urlStrs.length; j++) {
					_urlStrs[j] = "http://www.veryzhun.com/searchnum.asp?flightnum=" + flightList.get(i*num + j).getFno();
				}
				Spider.create(new PageProcessorVeryZhunFlighInfo(proxyip)).addUrl(_urlStrs).addPipeline(new PipelineVeryZhunFlightInfo()).start();
			}
		} else {
			for (int i = 0; i < urlStrs.length; i++) {
				Spider.create(new PageProcessorVeryZhunFlighInfo(proxyIpList.get(i))).addUrl(urlStrs[i]).addPipeline(new PipelineVeryZhunFlightInfo()).thread(20).start();
			}
		}*/
		//Spider.create(new PageProcessorVeryZhunFlighInfo()).addUrl("http://www.veryzhun.com/searchnum.asp?flightnum=CA1344").start();
		List<Proxyip> proxyIpList = Proxyip.dao.find("SELECT * FROM proxyip LIMIT 0,10");
		for (Proxyip proxyip : proxyIpList) {
			Spider.create(new PageProcessorVeryZhunFlighInfo(proxyip)).addUrl("http://www.veryzhun.com/searchnum.asp?flightnum=CA1344").start();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		/*Flightinfo flightinfo = Flightinfo.dao.findById(14339);
		String company = flightinfo.getCompany();
		try {
			byte[] companyDate = company.getBytes("ISO8859_1");
			for (byte b : companyDate) {
				System.out.println(b);
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		//System.out.println(flightinfo.getCompany());
	}

}
